package cn.sh.ideal.web.configuration.management.configuration.collection.controller;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import cn.sh.ideal.web.configuration.management.configuration.collection.entity.ConfigurationCollectionEntity;
import cn.sh.ideal.web.configuration.management.configuration.collection.entity.UpdateConfigurationCollectionEntity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

/**
 * 
 * @author zhaochuan.zhong
 *
 */
@Controller
@RequestMapping(value = "/data")
@PropertySource(value = { "classpath:jdbc.properties" })
public class ConfigurationCollectionDataController {
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	private RestTemplate rs = new RestTemplate();

	@Value("${server_address}")
	private String serverAddress;

	/**
	 * 获取指标配置信息。
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/selectConfiguationCollectionInfo", method = RequestMethod.POST)
	@ResponseBody
	public String getUserInfo(@RequestBody ConfigurationCollectionEntity paramter) {

		Integer size = paramter.getSize();
		Integer page = paramter.getPage();
		String monitorIndex = paramter.getMonitorIndex();
		String IndexKpi = paramter.getIndexKpi();
		String url = serverAddress+"/selectConfiguationCollectionInfo?size="
				+ size + "&page=" + page  + "&monitorIndex=" + monitorIndex + "&IndexKpi=" + IndexKpi ;
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);
		String reult = exchange.getBody();
		return reult;
	}
	
	/**
	 * 更新指标配置信息。
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/updateCollectionConfigInfo", method = RequestMethod.POST)
	@ResponseBody
	public String updateCollectionConfigInfo(@RequestBody String paramter) {
		ArrayList<UpdateConfigurationCollectionEntity> paramterList = (ArrayList<UpdateConfigurationCollectionEntity>) JSON.parseObject(paramter, new TypeReference<ArrayList<UpdateConfigurationCollectionEntity>>(){});
		String reult = "";
		for(UpdateConfigurationCollectionEntity item:paramterList){
			Integer id = item.getId();
			String dataAcquisitionFrequency = item.getDataAcquisitionFrequency();
			String monitorIndex = item.getMonitorIndex();
			String indexKpi = item.getIndexKpi();
			String times = item.getTimes();
			String minutes = item.getMinutes();
			String url = serverAddress+"/updateCollectionConfigInfo?id="
					+ id + "&dataAcquisitionFrequency="+ dataAcquisitionFrequency + "&monitorIndex=" + monitorIndex  + "&indexKpi=" + indexKpi
					+ "&times="+times + "&minutes=" + minutes;
			HttpHeaders headers = new HttpHeaders();
			MediaType type = MediaType
					.parseMediaType("application/json;charset=UTF-8");
			headers.setContentType(type);
			HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

			ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,entity, String.class);
			reult = exchange.getBody();
		}
		return reult;
	}
	
	/**
	 * 获取主机刷新信息。
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/selectConfiguationCollectionTab2Info", method = RequestMethod.POST)
	@ResponseBody
	public String selectConfiguationCollectionTab2Info(@RequestBody ConfigurationCollectionEntity paramter) {

		Integer size = paramter.getSize();
		Integer page = paramter.getPage();
		String hostIp = paramter.getHostIp();
		String hostName = paramter.getHostName();
		String url = serverAddress+"/selectConfiguationCollectionTab2Info?size="
				+ size + "&page=" + page  + "&hostIp=" + hostIp + "&hostName=" + hostName ;
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);
		String reult = exchange.getBody();
		return reult;
	}
	
	/**
	 * 更新指标配置信息。
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/updateCollectionConfigTab2Info", method = RequestMethod.POST)
	@ResponseBody
	public String updateCollectionConfigTab2Info(@RequestBody String paramter) {
		ArrayList<UpdateConfigurationCollectionEntity> paramterList = (ArrayList<UpdateConfigurationCollectionEntity>) JSON.parseObject(paramter, new TypeReference<ArrayList<UpdateConfigurationCollectionEntity>>(){});
		String reult = "";
		for(UpdateConfigurationCollectionEntity item:paramterList){
			Integer id = item.getId();
			Integer sytemParameterId =  item.getSytemParameterId();
			String refreshTime = item.getRefreshTime();
			String hostName = item.getHostName();
			String hostIp = item.getHostIp();
			String url = serverAddress+"/updateCollectionConfigTab2Info?id="
					+ id + "&hostName="+ hostName + "&hostIp=" + hostIp + "&refreshTime="+refreshTime +"&sytemParameterId="+sytemParameterId;
			HttpHeaders headers = new HttpHeaders();
			MediaType type = MediaType
					.parseMediaType("application/json;charset=UTF-8");
			headers.setContentType(type);
			HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

			ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,entity, String.class);
			reult = exchange.getBody();
		}
		return reult;
	}
	
	/**
	 * 下拉框数据取值。
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/getListOptionValue", method = RequestMethod.POST)
	@ResponseBody
	public String getSelectOptionValue() {

		String url = serverAddress+"/getListOptionValue";
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);
		String reult = exchange.getBody();
		return reult;
	}
	
	
	/**
	 * 查询数据采集频率
	 * creat by:zhangyy
	 * @param 
	 * @return reult
	 */
	@RequestMapping(value = "/queryDataAcquisitionFrequency", method = RequestMethod.POST)
	@ResponseBody
	public String queryDataAcquisitionFrequency() {

		String url = serverAddress+"/queryDataAcquisitionFrequency";
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);
		String reult = exchange.getBody();
		return reult;
	}
	
	/**
	 * 查询监控指标ztree根节点
	 * creat by:zhangyy
	 * @param 
	 * @return reult
	 */
	@RequestMapping(value = "/selectMonitoringIndex", method = RequestMethod.POST)
	@ResponseBody
	public String selectMonitoringIndex() {

		String url = serverAddress+"/selectMonitoringIndex";
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);
		String reult = exchange.getBody();
		return reult;
	}
	
	/**
	 * 查询监控指标ztree子节点异步加载
	 * creat by:zhangyy
	 * @param 
	 * @return reult
	 */
	@RequestMapping(value = "/asyncGetKPITreeData", method = RequestMethod.POST)
	@ResponseBody
	public String asyncGetKPITreeData(String id) {
		String url = serverAddress+"/asyncGetKPITreeData?id="+id;
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);
		String reult = exchange.getBody();
		return reult;
	}
	
	/**
	 * 修改数据采集频率
	 * creat by:zhangyy
	 * @param 
	 * @return reult
	 */
	@RequestMapping(value = "/updataDataAcquisitionFrequency", method = RequestMethod.POST)
	@ResponseBody
	public String updataDataAcquisitionFrequency(String dataAcquisitionFrequency) {
		String url = serverAddress+"/updataDataAcquisitionFrequency?dataAcquisitionFrequency="+dataAcquisitionFrequency;
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,
				entity, String.class);
		String reult = exchange.getBody();
		return reult;
	}
	
	/**
	 * 批量修改times AND minutes
	 * creat by:zhangyy
	 * @param 
	 * @return reult
	 */
	@RequestMapping(value = "/updataTimesANDMinutes", method = RequestMethod.POST)
	@ResponseBody
	public String updataTimesANDMinutes(@RequestBody String paramter) {
		ArrayList<UpdateConfigurationCollectionEntity> paramterList = (ArrayList<UpdateConfigurationCollectionEntity>) JSON.parseObject(paramter, new TypeReference<ArrayList<UpdateConfigurationCollectionEntity>>(){});
		String reult = "";
		for(UpdateConfigurationCollectionEntity item:paramterList){
			Integer id = item.getId();
			String times = item.getTimes();
			String minutes = item.getMinutes();
			String url = serverAddress+"/updataTimesANDMinutes?id="
					+ id + "&times="+times + "&minutes=" + minutes;
			HttpHeaders headers = new HttpHeaders();
			MediaType type = MediaType
					.parseMediaType("application/json;charset=UTF-8");
			headers.setContentType(type);
			HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

			ResponseEntity<String> exchange = rs.exchange(url, HttpMethod.GET,entity, String.class);
			reult = exchange.getBody();
		}
		return reult;
	}
}
